package com.byinsight.web.actions.expense;

import java.util.List;

import com.byinsight.service.dto.Expense;
import com.byinsight.service.dto.QueryDefinition;
import com.byinsight.web.actions.CheckSessionAction;
import com.byinsight.web.actions.util.ExpenseHelper;
import com.byinsight.web.actions.util.QueryDefinitionParser;

import eg.backend.domain.fly.AggregatedRecord;

public class QueryExpenseAction extends CheckSessionAction {

	private static final long serialVersionUID = -1423240083027136641L;

	private String qd;
	private boolean detail;
	
	public String getQd() {
		return qd;
	}

	public void setQd(String qd) {
		this.qd = qd;
	}

	public boolean isDetail() {
		return detail;
	}

	public void setDetail(boolean detail) {
		this.detail = detail;
	}

	@Override
	protected String process() {
		QueryDefinition queryDefinition = parseQueryDefinition();
		String ret = "";
		if (isDetail()) {
			List<Expense> expenses = expenseService.findDetail(queryDefinition);
			ret = ExpenseHelper.toXml(expenses);
		} else {
			List<AggregatedRecord> aggregated = expenseService.findAggregated(queryDefinition);
			ret = ExpenseHelper.toAggregatedXml(aggregated);
		}
		return ret;
	}
	
	private QueryDefinition parseQueryDefinition() {
		QueryDefinitionParser parser = new QueryDefinitionParser();
		parser.parse(getQd());
		QueryDefinition ret = parser.getQd();
		return ret;
	}

}
